<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('时间')" prop="closePrice">
            <DatePicker v-model="form.closingDate" :placeholder="$t('时间')" @change="changePrice(form.closePrice, $event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收盘价（元/股）')" prop="closePrice">
            <Money v-model="form.closePrice" :placeholder="$t('收盘价')" @change="changePrice($event, form.closingDate)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('涨跌幅')" prop="chg">
            <Money v-model="form.chg" :placeholder="$t('涨跌幅')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收益率%')" prop="yield">
            <Money v-model="form.yield" :placeholder="$t('收益率')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收益金额（元）')" prop="revenueAmount">
            <Money v-model="form.revenueAmount" :placeholder="$t('收益金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最新市值')" prop="latestValue">
            <Money v-model="form.latestValue" :placeholder="$t('最新市值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('市值变动')" prop="valueChange">
            <el-input v-model="form.valueChange" :placeholder="$t('市值变动')" show-word-limit />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:price" />
    </div>
  </div>
</template>

<script>
import {
  saveDealSpoClosePrice,
  getDealSpoClosePrice,
  getTotalShareByDealId, getClosePriceByDate, getChgByClosePrice, getYieldByClosePrice, getAmountByClosePrice
} from '@/api/deal/spo/closePrice/dealSpoClosePrice'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  closingDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  closePrice: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealSpoClosePrice(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    closingDate: undefined,
                    closePrice: undefined,
                    chg: undefined,
                    yield: undefined,
                    revenueAmount: undefined,
                    latestValue: undefined,
                    valueChange: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            // 最新收盘价变动, 同步修改自动计算值
            changePrice(val, time) {
              if (val) {
                // 最新市值	最新市值=最新收盘价*公司总股本
                getTotalShareByDealId(this.$route.query.id).then(response => {
                  if (response.data) {
                    this.form.latestValue = response.data * val
                  }
                })
              }
              if (val && time) {
                // 市值变动	当日市值-上一日市值
                getClosePriceByDate(time).then(response => {
                  if (response.data) {
                    this.form.valueChange = val - response.data
                  }
                })
                getChgByClosePrice(this.form).then(response => {
                  if (response.data) {
                    this.form.chg = response.data
                  }
                })
                getYieldByClosePrice(this.form).then(response => {
                  if (response.data) {
                    this.form.yield = response.data
                  }
                })
                getAmountByClosePrice(this.form).then(response => {
                  if (response.data) {
                    this.form.revenueAmount = response.data
                  }
                })
              }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealSpoClosePrice(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
